<?php
namespace App\HttpController;

use App\Common\GameCommon;
use App\Utility\BaseEnum;
use App\Model\User;
use App\Common\AuthCommon;
use App\Enum\MahjEnum;
use App\Enum\CardEnum;
use App\Enum\PorkerEnum;
use App\GameService\games\PlayGrabNine;
use App\Common\SumComm;

/**
 * Class Index
 *
 * @package App\HttpController
 */
class TestDebug
{

    /**
     * 首页方法
     *
     * @author : evalor <master@evalor.cn>
     */
    function index()
    {}

    /**
     * 推送
     */
    function debug($mtype = 0)
    {
        echo '<pre>';
        if ($mtype == 1) {
            $this->debugNiuNiu();
        } else if ($mtype == 2) {
            $this->dealNiuNiu();
        } else if ($mtype == 3) {
            $this->getOrder();
        } else if ($mtype == 4) {
            $this->loginNewUser();
        }else if ($mtype == 8){
            //防止以小博大计算
            $this->dealPlayPrice();
        }else if ($mtype == 10){
            //数据统计测试
            $this->sumAgentData();
        }
        else {
            exit('非法调试');
        }
    }

    // 登录新用户
    function loginNewUser()
    {
        // 完成用户注册
        $userModel = new User();
        // 登录用户；默认代理为10015；
        $userInfo = [];

        $num = 1000;
        $agentId = 10001;
        $agentName = 'yifeng01';
        for ($i = 10; $i <= $num; $i ++) {
            $userName = 'yifeng' . $i;
            // 新用户数据；
            $userInfo[] = array(
                'name' => $userName,
                'password' => '8677D2D15C655180174AA354B897FCA9',
                'group_id' => 1,
                'status' => 1,
                'recUid' => $agentId,
                'recUser' => $agentName,
                'ancestor_ids' => "10000," . $agentId, // 上级ID值；
                'level' => 1,
                'realname' => $userName,
                'gender' => 0,
                'user_ident' => 1,
                'head_url' => rand(1, 7),
                'points' => 50000,
                'reg_ip' => BaseEnum::getClientIP()
            );
        }
        BaseEnum::Logger("新加入玩家:playname={$userName} ,insertRes=" . json_encode($userInfo), "user-login");
        // 插入一条数据
        $insertRow = $userModel->dbConnector()->insertMulti($userModel->tableName, $userInfo);
        var_dump($insertRow);
        exit();

        $apiUrl = 'http://192.168.0.147:9501/gamehandler/';
        $agentId = 10015;
        $account = "xch02";
        $desckey = 'ABC1232134123SEE';
        $md5key = 'MYM23456';

        // var_dump(BaseEnum::descDecode($desckey, 'ym0d/4ea32xbRkus4EmJG2/ vLRkVXkfBSGtKTB5AsJTn4GJ1AcUlvso74b0xMkbPDtnOAqLP8xan0SiN93K75h+fISiwqHe jFcNq5rDNQ='));

        $nowtime = BaseEnum::getNowTime(FALSE);
        // 提交格式：（s=0&account=111111&money=100&orderid=1000120170306143036949111111&ip=127.0.0.1&lineCode=text11&KindID=0）
        $jsondata = [
            'm' => 1,
            "a" => $agentId,
            "account" => $account,
            "ip" => '127.0.0.1',
            "lineCode" => '0',
            "GId" => 0
        ];
        $signkey = strtoupper(md5("{$agentId}{$nowtime}{$md5key}"));

        // 指定参数；
        $param = BaseEnum::descEncode($desckey, json_encode($jsondata));

        // 组合URL地址；
        $fullUrl = $apiUrl . $agentId . '?stamp=' . $nowtime . "&param=" . $param . "&key=" . $signkey;

        var_dump($fullUrl);

        $data = file_get_contents($fullUrl);

        var_dump($data);
        exit();
    }

    // 二八发牌；算牌测试
    function debugTowEight()
    {
        $num = 1;
        $allCards = [];
        do {
            $useCards2 = MahjEnum::createRoomGameTWOEBAR(4, [
                "everyCard" => 2,
                "gameJu" => "2000-162"
            ], $num - 1);
            echo "<br/>{$num} === 发牌：" . BaseEnum::JsonEncode($useCards2);
            $allCards[$num] = json_encode($useCards2);
            $num ++;
        } while ($num <= 5);

        echo '<br/>所有牌：' . print_r($allCards, true);
        exit();

        // 结算牌值；
        $cardList = [];
        foreach ($useCards2 as $r => $card) {
            $cardList[$r] = MahjEnum::dealTwoEightLevel($card); // 结算单个号码
        }
        // print_r($cardList);
        // 结算牌数据：

        $zjdata = json_decode('{"seatNo":0,"cards":[9,4],"step":1,"remark":"","userId":10002,"mainFlag":1}', true);
        $xjdata = json_decode('{"1":{"seatNo":1,"cards":[1,5],"step":1,"remark":"","userId":10001,"mainFlag":0}}', true);
        $fjdata = json_decode('{"rid":4,"gpKey":"TWOEBAR","gameId":102,"roomId":1020,"roomName":"\u4f53\u9a8c\u623f","status":1,"gfFullName":"\u4e8c\u516b\u6760\u4f53\u9a8c\u623f","playNumber":2,"smallPoints":"10.00","everyPoints":"5.00","remark":null,"minPlayer":2,"maxPlayer":4,"minMultiple":1,"maxMultiple":1}', true);
        $two = $one = reset($xjdata);
        $two = array_merge($two, [
            'seatNo' => 3,
            'userId' => 1004,
            'cards' => [
                20,
                36,
                27,
                43,
                60
            ]
        ]);
        $one = array_merge($one, [
            'seatNo' => 4,
            'userId' => 1005,
            'cards' => [
                59,
                44,
                23,
                21,
                24
            ]
        ]);
        $returnd = false;
        // print_r($xjdata);exit ;
        echo json_encode($zjdata) . '<br/>';
        echo json_encode($xjdata) . '<br/>';
        $basedata = PlayTwoEight::dealRoomGameTWOEBAR($zjdata, $xjdata, $fjdata, $returnd);
        echo print_r($basedata, true) . '<br/>';
        echo print_r($returnd, true) . '<br/>';

        exit();
    }

    // 德洲扑克 发牌数据
    function debugDZPorker()
    {
        $num = 1;
        $useCards2 = CardEnum::createRoomGameDZPK(4, [
            "everyCard" => 2,
            "gameJu" => ""
        ], $num - 1);
        echo '<br/>德洲扑克发牌内容：' . json_encode($useCards2, true);

        // 先牌；
        $onecards = [
            44,
            22,
            51,
            61,
            33
        ];
        $secondcards = [
            43,
            23,
            52,
            60,
            30
        ];

        echo '<br/>解析牌' . print_r($onecards, true);

        // 比牌操作；
        // $nums = PorkerEnum::parseOneCardDottod($onecards);
        $onecards = [
            0x05,
            0x02,
            0x07,
            0x09,
            0x0C
        ];
        $secondcards = [
            0x32,
            0x35,
            0x37,
            0x39,
            0x3C
        ];

        // $lastRes = PorkerEnum::combCardSize($onecards,$secondcards);

        // 排列组合值：
        $allcards = [
            0x05,
            0x27,
            0x07,
            0x09,
            0x19,
            0x28,
            0x38
        ];
        // $allcards = [1,21,37,57,4];

        print_r($allcards);
        $lastRes = PorkerEnum::getPlayMaxCards($allcards, 5);

        // 大小比要加排序处理()
        //{"seatNo":2,"uid":10020,"maxCards":"20,4,13,17,54","cardLevel":2,"pLevel":"0x24ed60"},{"seatNo":4,"uid":10003,"maxCards":"20,4,13,45,22","cardLevel":3,"pLevel":"0x3d4600"}
        $players = array(
            0 => array(
                'seatNo' => 0,
                'uid' => 10003,
                'maxCards' => '20,4,13,45,22',
                'cardLevel' => 3,
                'pLevel'=> '0x3d4600'
            ),
            1 => array(
                'seatNo' => 2,
                'uid' => 10017,
                'maxCards' => '20,4,13,17,54',
                'cardLevel' => 2,
                'pLevel'=>'0x24ed60'
            ),
            2 => array(
                'seatNo' => 3,
                'uid' => 10002,
                'maxCards' => '41,59,22,55,9',
                'cardLevel' => 2,
                'pLevel'=>'0x29b760'
            ),
            3 => array(
                'seatNo' => 4,
                'uid' => 10018,
                'maxCards' => '39,54,55,10,22',
                'cardLevel' => 3,
                'pLevel'=>'0x376a00'
            )
        );
        
        $allNums = $firstNum = false;
        
        $lastRes = PorkerEnum::playCombSize($players,$allNums, $firstNum);
        print_r($lastRes);
        var_dump($allNums);
        var_dump($firstNum);
        exit();
        exit();
        ;
    }

    // 牌九发牌；算牌测试
    function debugPai9()
    {
        $num = 1;
        $allCards = [];

        $allleve = MahjEnum::getGrabDuiConfig();
        // print_r(array_column($allleve, 'name','key'));exit ;

        do {
            $useCards2 = MahjEnum::createRoomGameQZGRAB(4, [
                "everyCard" => 2,
                "gameJu" => "2000-163"
            ], $num - 1);
            echo "<br/>{$num} === 发牌：" . BaseEnum::JsonEncode($useCards2, 0);
            $allCards[$num] = json_encode($useCards2);
            $num ++;
        } while ($num <= 2);

        echo '<br/>所有牌：' . print_r($allCards, true);

        // 结算牌值；
        $cardList = [];
        foreach ($useCards2 as $r => $card) {
            $levfv = FALSE;
            $cardList[$r] = MahjEnum::dealGrabNineLevel($card, $levfv); // 结算单个号码
        }
        print_r($cardList);
       // exit();

        // 结算牌数据：

        $zjdata = json_decode('{"seatNo":0,"cards":[88,25],"current":1,"step":1,"userId":10001,"mainFlag":1,"mainMutle":1}', true);
        $xjdata = json_decode('{"1":{"seatNo":1,"cards":[84,3],"current":1,"step":1,"userId":10002,"mainFlag":0,"mainMutle":1}}', true);
        $fjdata = json_decode('{"rid":36,"gpKey":"QZGRAB","gameId":112,"roomId":1120,"roomName":"\u4f53\u9a8c\u623f","status":1,"gfFullName":"\u62a2\u5e84\u724c\u4e5d\u4f53\u9a8c\u623f","playNumber":1,"smallPoints":"5.00","maxInPoints":"0.00","everyPoints":"2.00","remark":null,"minPlayer":2,"maxPlayer":4,"minMultiple":1,"maxMultiple":66}', true);
        $two = $one = reset($xjdata);
        $two = array_merge($two, [
            'seatNo' => 3,
            'userId' => 1004,
            'cards' => [
                20,
                36,
                27,
                43,
                60
            ]
        ]);
        $one = array_merge($one, [
            'seatNo' => 4,
            'userId' => 1005,
            'cards' => [
                59,
                44,
                23,
                21,
                24
            ]
        ]);
        $returnd = false;
        // print_r($xjdata);exit ;
        echo json_encode($zjdata) . '<br/>';
        echo json_encode($xjdata) . '<br/>';
        $basedata = PlayGrabNine::dealRoomGameQZGRAB($zjdata, $xjdata, $fjdata, $returnd);
        echo print_r($basedata, true) . '<br/>';
        echo print_r($returnd, true) . '<br/>';

        exit();
    }

    /**
     * 记算抢庄牛牛的大小；
     *
     * 牛牛牌大小： 五小牛，四炸，五花牛，四花牛，牛牛，牛九....>牛一，没牛
     */
    function debugNiuNiu()
    {
        // $this->debugTowEight();
        $this->debugDZPorker();
        exit();
        // $this->debugPai9();exit ;

        $playerNum = 5;
        // 启动发牌；======> 操作代码 ： XYM_0X17；
        $useCards = CardEnum::createRoomGameQZNN($playerNum, [
            "gameJu" => ''
        ], true); // 5张牌；

        // 计算牌的数据；
        foreach ($useCards as $r => $card) {
            $numtime = 0;
            $cardinfo = CardEnum::dealNiuNiuLevel($card, $numtime);
            $useCards[$r] = array_merge($cardinfo, [
                "cards" => implode(",", $card)
            ]);
        }
        BaseEnum::Logger("记录日志：" . json_encode($useCards), self::class);
        print_r($useCards);
        $paival = [
            25,
            24,
            55,
            11,
            27
        ];
        $numtime = 0;
        $res = CardEnum::dealNiuNiuLevel($paival, $numtime); // 牛牛的计算规则；

        var_dump($res);
        exit();
    }

    // 结算
    function dealNiuNiu()
    {
        // 启动发牌；======> 操作代码 ： XYM_0X17；
        $useCards = CardEnum::createRoomGameQZNN(4, [
            "everyCard" => 5
        ], true); // 5张牌；

        $useCards = array(
            1 => [
                50,
                34,
                18,
                2,
                19
            ],
            2 => [
                42,
                37,
                25,
                61,
                50
            ]
        );

        // 四炸：
        $useCards = array(
            1 => [
                0x02,
                0x13,
                0x23,
                0x33,
                0x24
            ],
            2 => [
                0x04,
                0x14,
                0x24,
                0x34,
                0x31
            ]
        );

        // print_r(implode(',', [0x02,0x12,0x22,0x32]));exit ;

        // 计算牌的数据；
        foreach ($useCards as $r => $card) {
            $numtime = 0;
            $cardinfo = CardEnum::dealNiuNiuLevel($card, $numtime);
            $useCards[$r] = array_merge($cardinfo, [
                "cards" => implode(",", $card)
            ]);
        }

        // print_r($useCards);exit ;

        // 启动牌
        $zjdata = json_decode('{"p_fd":15,"seatNo":0,"cards":[ 2,18,34,50,33],"userId":10002,"mainFlag":1,"userPoints":200}', true);
        $xjdata = json_decode('{"1":{"p_fd":14,"seatNo":1,"cards":[4,20,36,52,49],"userId":10001,"mainFlag":0,"userPoints":200}}', true);
        $fjdata = json_decode('{"rid":1,"gpKey":"QZNN","gameId":101,"roomId":1010,"roomName":"\u521d\u7ea7\u623f","status":1,"gfFullName":"\u62a2\u5e84\u725b\u725b\u521d\u7ea7\u623f","smallPoints":"10.00","everyPoints":"10.00","remark":null,"minPlayer":2,"maxPlayer":4,"minMultiple":1,"maxMultiple":1}', true);
        $two = $one = reset($xjdata);
        $two = array_merge($two, [
            'seatNo' => 3,
            'userId' => 1004,
            'cards' => [
                20,
                36,
                27,
                43,
                60
            ]
        ]);
        $one = array_merge($one, [
            'seatNo' => 4,
            'userId' => 1005,
            'cards' => [
                59,
                44,
                23,
                21,
                24
            ]
        ]);
        /*
         * $xjdata[3] = $two;
         * $xjdata[4] = $one;
         */

        $returnd = false;
        // print_r($xjdata);exit ;
        echo json_encode($zjdata) . '<br/>';
        echo json_encode($xjdata) . '<br/>';
        $basedata = CardEnum::dealRoomGameQZNN($zjdata, $xjdata, $fjdata, $returnd);
        echo "结算结果>>>" . print_r($basedata, true) . '<br/>';

        exit();

        $errorCode = false;
        $room_No = '0520105217108075-193';
        // 结算订单数据；
        //$result = GameCommon::dealUserOrder(1010, $room_No, $basedata, $returnd, $errorCode);
        print_r($result);
        exit();
    }

    function getOrder()
    {
        echo "<pre>获取玩家的订单数据>>>";
        $userId = 10001;
        $gameId = 101;
        $list = GameCommon::getUserPlayOrder($userId, $gameId, 10);

        print_r($list[0]);

        $key = 'ABC1232134123SEE';
        $ssid = BaseEnum::descEncode("USERS", "10006"); // 存储的用户ID；
        $value = json_encode([
            "m" => 1,
            "a" => 10015,
            "u" => "test001",
            "sid" => $ssid
        ]);
        $jami_str = BaseEnum::descEncode($key, $value);

        echo "调试加密算法>>>>字段串：{$value}  加密后数据：" . $jami_str;
        $jiemi_str = BaseEnum::descDecode($key, $jami_str);
        echo "<br>调试加密算法>>>>字段串：{$jami_str}  解密后数据：" . $jiemi_str;

        echo "<br>登录用户：test001>>>";
        // websocket 登录验证
        $lsr = AuthCommon::loginWSGames("10015_test001", $jami_str);

        var_dump($lsr);
    }


    //防止以小博大计算
    function dealPlayPrice() {
        
        //$this->debugPai9();
        echo "原结算数据：";
        
        BaseEnum::pingCount('192.168.0.147');
        
        
        $gameId = 112;
        $data = json_decode('[{"uid":12013,"seatNo":0,"mainFlag":0,"betMutal":4,"money":-2640,"eachprice":2640,"level":5,"card_mutle":1,"bet":"\u5e84","cards":[68,49],"water":0},{"uid":12081,"seatNo":1,"mainFlag":1,"betMutal":66,"money":2640,"eachprice":2640,"level":6,"card_mutle":1,"zj":"\u95f2","cards":[48,54],"water":132}]',true);
        print_r($data);
        
        $res = GameCommon::checkUserDealPrice($gameId,$data,12081);
        echo "处理后的数据：res = $res data=".print_r($data,true);
        exit ;
    }
   
    
    //代理统计数据
    function sumAgentData() {
        echo "进入数据统计调试：";
        $agentId = 10015;
        //$sumValue = SumComm::sumOneAgentLineFields($agentId);
        $sumValue = SumComm::sumALLAgentLineData();
        
        print_r($sumValue);
        
        ;
    }
}
